//
//  HCMessageCenterView.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/1/19.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCMessageCenterView.h"

#import "HCMessageCenterTableViewCell.h"

#import "HCMsgCenterModel.h"


@interface HCMessageCenterView () <UITableViewDelegate,UITableViewDataSource> {
    UITableView *tableView_;
    HCMsgCenterModel *model_;
}

@end

@implementation HCMessageCenterView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initializeSubViews:frame];
    }
    return self;
}

-(void)initializeSubViews:(CGRect)frame {
    tableView_ = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    tableView_.backgroundColor = YBKRGBCOLOR(242, 242, 242, 1);
    tableView_.delegate = self;
    tableView_.dataSource = self;
    tableView_.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView_.rowHeight = 70;
    tableView_.showsVerticalScrollIndicator = NO;
    [self addSubview:tableView_];
    
    [tableView_ registerNib:[UINib nibWithNibName:@"HCMessageCenterTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"messagecenter"];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (model_) {
        return model_.message.count;
    }else {
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HCMessageCenterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"messagecenter" forIndexPath:indexPath];
    
    if (model_) {
        HCMsgCenterData *data = model_.message[indexPath.row];
        cell.model = data;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([self.delegate respondsToSelector:@selector(messageCenterCellClicked:)]&&self.delegate) {
        [self.delegate messageCenterCellClicked:indexPath];
    }
}

-(void)setModel:(id)model {
    model_ = (HCMsgCenterModel *)model;
    [tableView_ reloadData];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
